package com.yanqu.road.server.manger.rebate;

import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.rebate.ChinaUserRebate;
import com.yanqu.road.logic.bussiness.player.UserRebateBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


//国内返利
public class ChinaRebateMgr extends TempMgr {

    //openId
    private static Map<String, ChinaUserRebate> chinaUserRebateMap = new ConcurrentHashMap<>();

    private static Map<String, ChinaUserRebate> serverChinaUserRebateMap = new ConcurrentHashMap<>();


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }


    public static ChinaUserRebate getChinaUserRebate(String openId){
        if(serverChinaUserRebateMap.containsKey(openId)){
            if(chinaUserRebateMap.containsKey(openId)){
                return chinaUserRebateMap.get(openId);
            }
            ChinaUserRebate chinaUserRebate = UserRebateBussiness.getServerChinaUserRebate(openId);
            if(chinaUserRebate == null){
                return null;
            }else{
                if(UserRebateBussiness.updateServerChinaUserRebate(openId)){
                    chinaUserRebateMap.put(chinaUserRebate.getOpenId(),chinaUserRebate);
                }
            }
            return chinaUserRebateMap.get(openId);
        }else{
            return null;
        }
    }

    public boolean save() {
        for(ChinaUserRebate chinaUserRebate : chinaUserRebateMap.values()){
            if(chinaUserRebate.getOp() == DataStatus.Update){
                UserRebateBussiness.updateChinaUserRebate(chinaUserRebate);
            }else if(chinaUserRebate.getOp() == DataStatus.Insert){
                UserRebateBussiness.addChinaUserRebate(chinaUserRebate);
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        serverChinaUserRebateMap = UserRebateBussiness.getServerChinaUserRebateMap();
        chinaUserRebateMap = UserRebateBussiness.getChinaUserRebateMap();
        return true;
    }
}
